package com.fist.eth.flutter_plugin_eth_wallet.utils

import android.util.Log
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import org.web3j.protocol.Web3j
import org.web3j.protocol.Web3jFactory
import org.web3j.protocol.http.HttpService
import java.util.concurrent.TimeUnit

object Web3jUtils {
    fun getWeb3j(url:String): Web3j {
        return Web3jFactory.build(HttpService(url, createOkHttpClient(), false))
    }

    private fun createOkHttpClient(): OkHttpClient {
        val builder = OkHttpClient.Builder()
        builder.writeTimeout((30 * 1000).toLong(), TimeUnit.SECONDS)
                .connectTimeout((30 * 1000).toLong(), TimeUnit.SECONDS)
        configureLogging(builder)
        return builder.build()
    }

    private fun configureLogging(builder: OkHttpClient.Builder) {
        val logging = HttpLoggingInterceptor(
                HttpLoggingInterceptor.Logger { msg -> Log.i("TAG", "log: $msg") })
        logging.level = HttpLoggingInterceptor.Level.BODY
        builder.addInterceptor(logging)
    }
}